<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\App;
use think\facade\View;
use addons\Builder\facade\Builder;

class Config extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Config';

    //验证器
    protected string $validate = 'Config';

    public function index(): string
    {
        return Builder::Table()
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'name', 'title' => '名称'])
            ->addColumn(['field' => 'title', 'title' => '标题'])
            ->addColumn(['field' => 'type', 'title' => '类型'])
            ->addColumn(['field' => 'group', 'title' => '分组'])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '修改时间'])
            ->addColumn(['field' => 'status', 'title' => '状态', 'templet' => '#status', 'width' => 120])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作'])
            ->addTopButtons(['add' => ['size' => '60%']])
            ->addRightButtons(['edit' => ['size' => '60%'], 'delete'])
            ->addSwitchTool([['name' => 'status', 'pk' => $this->pk, 'text' => '开启|关闭']])
            ->addSearch([['name__title', '名称/标题', 'input,Like']])
            ->fetch();
    }

    public function create($id = 1): string
    {

        $map = array('status' => 1, 'group' => $id);
        View::assign('list', $this->model::where($map)->order('sort', 'asc')->select());
        View::assign('id', $id);
        return View::fetch();
    }

    public function saveGroup(): \think\response\Json
    {
        $param = $this->request->post();
        $config = $param['config'];
        unset($param['config']);
        foreach ($param as $key => $value) {
            $config[$key] = $value;
        }
        return json($this->model::saveGroup($config, $this->admin));
    }

    public function add($tpl = 'config/add'): string
    {
        return parent::add($tpl); // TODO: Change the autogenerated stub
    }


    public function edit($id = '', $tpl = 'config/edit', $info = ''): string
    {
        return parent::edit($id, $tpl, $info); // TODO: Change the autogenerated stub
    }


}
